import { SUCCESS } from '../../yali-sdk/utils/success';
import { FAIL } from '../../yali-sdk/utils/fail';
import { getExtConfig } from '../../utils/checkAuth';
import getIn from '../../yali-sdk/utils/getIn';
import apiLogin from '../../yali-sdk/business/live/api/apiLogin';
import apiGetWxOpenId from '../../yali-sdk/business/live/api/apiGetWxOpenId';
Page({
    data:{
      phone:'',
      password:''
    },
   register(){
       wx.navigateTo({ url: '/pages/regist/regist' });
   },
   bandMobile(e) {
        const phone = e.detail.value;
        phone ? this.setData({ 'phone': e.detail.value}) : this.setData({ 'phone': '',});
    },
  bandPassword(e) {
        const password = e.detail.value;
        password ? this.setData({ 'password': e.detail.value}) : this.setData({ 'password': '',});
    },
   login_in(e){
      wx.showLoading();
      const self =this;
     if(!self.data.phone){
          wx.hideLoading();
           wx.showToast({
             title: '手机不能为空',
             icon: 'none',
             duration: 1500,
       });
       return;
     }
     if (!(/^1[34578]\d{9}$/.test(self.data.phone))) {
                    wx.hideLoading();
                    wx.showModal({
                    content: '手机号码格式错误' });
                    return;
           } 
   if(!self.data.password){
        wx.hideLoading();
           wx.showToast({
             title: '密码不能为空',
             icon: 'none',
             duration: 1500,
       });
       return;
     }
      if (!(/^1[34578]\d{9}$/.test(self.data.password))) {
                    wx.hideLoading();
                    wx.showModal({
                    content: '密码格式错误' });
                    return;
           } 
           if(self.data.password!=self.data.phone){
                    wx.hideLoading();
                    wx.showModal({
                    content: '密码不正确' });
                    return;
           }
      
           wx.login({
                success: (res) => {
                    let code =res.code;
                    if (res.errMsg == 'login:ok') {
                       wx.getUserInfo({
                            success: (reg) => {
                                  const appid = getIn(getExtConfig(), ['extAppid']);
                                if(reg.errMsg=='getUserInfo:ok' && reg.userInfo){
                                     let  name  = reg.userInfo.nickName;
                                     let  head_photo  = reg.userInfo.avatarUrl;
                                       global.actionDispatcher.execute(apiGetWxOpenId, { wx_code:code, appid }, (f, fs) => {
                                            if (f === SUCCESS) {
                                            let openId = fs.data.openid;
                                            global.actionDispatcher.execute(apiLogin, { mobile:self.data.phone,password:self.data.password,openId:openId,name:name,head_photo:head_photo}, (f, fs) => {
                                            if (f === SUCCESS) {
                                                wx.hideLoading();
                                                wx.showToast({
                                                    title: '登录成功',
                                                    icon: 'none',
                                                    duration: 1500,
                                                });   
                                                const url = '/pages/list/list';
                                                wx.reLaunch({ url });
                                            
                                            } else if (f === FAIL) {
                                                wx.hideLoading();
                                                wx.showToast({ title: fs.msg, icon: 'none', duration: 1500 });
                                            return;
                                            }
                                        });
                                    } else if (f === FAIL) {
                                        wx.hideLoading();
                                        wx.showToast({ title: '获取openId失败', icon: 'none', duration: 1500 });
                                        return;
                                    }
                                });
                               }
                             }
                         })
                    }
                },
                fail: (res) => {
                    wx.hideLoading();
                    wx.showToast({ title: '授权失败', icon: 'none', duration: 1500 });
                    return;
                },
            });
   } 
})